<%= if @show_modal do %>
  {live_modal(@modal.body.module_or_template,
    source: @source,
    title: @modal.body.title,
    view: @modal.body[:view],
    id: @modal.body.id,
    return_to: false
  )}
<% end %>
<div class="subhead ">
  <div class="container mx-auto">
    <h5>~/logs/<.link navigate={~p"/sources/#{@source}"} class="text-primary"><%= @source.name %></.link>/rules</h5>
    <div class="log-settings">
      <ul>
        <li>
          {LqlHelpers.lql_help_modal_link()}
        </li>
        <li>
          {LqlHelpers.bq_source_schema_modal_link()}
        </li>
      </ul>
    </div>
  </div>
</div>
<div id="lql-rules-container" class="content container mx-auto">
  <h5 class="header-margin">Source Routing Rules</h5>
  <p>Route logs with rules. Rules will match against a log event and copy it to the specified sink (another source).
    Save special entries with rules. Create a rule for signups, or bots or IP addresses using regular expressions.</p>
  <p><i class="fas fa-exclamation-circle"></i> An event can only be routed once. Rules will never match an event generated by another rule.</p>
  <ul class="list-group">
    <%= if Enum.empty?(@rules) do %>
      <li class="list-group-item">
        <div>No rules yet...</div>
      </li>
    <% end %>
    <%= for rule <- @rules, rule.sink do %>
      <li class="list-group-item">
        <% source = Enum.find(@sources, &(&1.token === rule.sink)) %>
        <a href="#" phx-click="delete_rule" phx-value-rule_id={rule.id}>
          <i class="fa fa-trash"></i>
        </a>
        Matching LQL <code>{rule.lql_string}</code>
        routing to <code>{source.name}</code>
      </li>
    <% end %>
  </ul>
  <.form :let={f} for={%{}} as={:rule} action="#" phx-submit="fsubmit">
    <.live_component id="rules-form" module={LogflareWeb.Lql.LqlFormLVC} f={f} lql_string={@lql_string} />
    <div class="form-group">
      {label(f, :sink, "Sink source", class: "control-label")}
      <% sink_form_options = for s <- @sources, do: [key: s.name, value: s.token, disabled: s.disabled] %>
      {select(f, :sink, sink_form_options, prompt: "Choose a sink", class: "form-control rules-input")}
      {error_tag(f, :sink)}
    </div>
    <div class="form-group">
      <%= submit id: :rule, class: "btn btn-primary" do %>
        Add Rule
      <% end %>
    </div>
  </.form>
</div>
